from flask import jsonify,Blueprint,request
from flask import current_app as apps
import json
from db import db1
import datetime
import os

bp_com = Blueprint('com',__name__)

def get_data():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    return data

@bp_com.route('/test')
async def test():
    str="请求ip:"+request.remote_addr +',请求的url：'+request.url+'错误信息：'+'\n'
    apps.logger.error(str)
    return "hello24324234"

@bp_com.route('/org_add',methods=['POST','GET'])
def orgadd_():
    if request.method == 'POST':
        data = get_data()
        sql1 = '''select name from organization where name ="%s" '''%(data['name'])
        d = db1.find(sql1)
        if not d:
            time1 = datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H:%M:%S')
            sql = '''insert into organization(name,coding,createpeople,createtime) values('%s','%s','%s','%s') '''\
                  %(data['name'],data['coding'],data['createpeople'],time1)
            db1.update(sql)
            return jsonify({"code":200,'mes':"添加成功"})
        else:
            return jsonify({"code":10010,'mes':"公司名已被注册"})
    else:
        sql = '''select * from organization'''
        s = db1.fid_all(sql)
        return jsonify({"code":200,'list':s})


from xlrd import open_workbook
from utils.myfile import myf
@bp_com.route('/file_add',methods = ['POST','GET'])
def file_add():
    if request.method=='POST':
        file = request.files['file']
        newname = myf.rename(file.filename)
        #上传文件
        file.save(os.path.join('./static/files/'+newname))

        name = 'static/files/'+newname
        bk = open_workbook(name, encoding_override='utf-8')
        sh = bk.sheets()[0]
        nrows = sh.nrows
        # ncols = sh.ncols
        for i in range(1, nrows):
            row_date = sh.row_values(i)
            print(row_date[0])
            print(row_date[1])
        return 'ok'



